package org.custom.spring.boot.datasource.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.custom.spring.boot.datasource.entity.datasource.CustomDataSourceProperties;
import org.custom.spring.boot.datasource.service.DataSourceService;
import org.custom.spring.boot.datasource.utils.SupportDataSourceType;
import org.custom.spring.boot.entity.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据源操作网络接口
 *
 * @author 李坤
 * @date 2022/3/31 10:54
 */
@RestController
@RequestMapping("/datasource")
@Api(tags = "数据源配置相关网络接口")
public class DataSourceController {
    @Resource
    private DataSourceService dataSourceService;

    @GetMapping("/supports")
    @ApiOperation(value = "获取支持的数据源类型")
    public Result<List<SupportDataSourceType.DataSourceType>> getDataSourceTypes() {
        return Result.success(SupportDataSourceType.getDataSourceTypes());
    }

    @GetMapping("/obtain")
    @ApiOperation(value = "获取当前用户数据源配置")
    public Result<List<CustomDataSourceProperties>> obtainCustomDataSourceProperties() {
        return Result.success(dataSourceService.obtainWithLoadCustomDataSourceProperties());
    }

    @PostMapping("/connect")
    @ApiOperation(value = "测试数据源连接接口")
    public Result<Boolean> connectTargetDataSource(@RequestBody CustomDataSourceProperties dataSourceProperties) {
        return Result.success(dataSourceService.try2ConnectDataSource(dataSourceProperties));
    }

    @PostMapping("/append")
    @ApiOperation(value = "添加数据源配置接口")
    public Result<Boolean> appendDynamicDataSource(@RequestBody CustomDataSourceProperties dataSourceProperties) {
        return Result.success(dataSourceService.saveCustomDataSourceProperties(dataSourceProperties));
    }
}
